<?php
namespace Home\Controller;
use Think\Controller;
class CartController extends Controller{
	public function add(){
		$cartModel = D('Home/Cart');
		//var_dump($cartModel);die();
		if(IS_POST){
			//var_dump(I('post.'));die;
			if($cartModel->addToCart(I('post.goods_id'),I('post.goods_number'))){
				$this->success('添加购物车成功',U('lst'));
			}
		}else{
			$this->error($model->getError);
		}
	}
	public function lst(){
		$cartModel = D('Home/Cart');
	//	$cartModel->moveDataToDb();
		$cartData = $cartModel->cartList();
		//echo '<pre>';
		//var_dump($cartData);die();
	    $this->assign(array(
	    	'cartData'=>$cartData,
	    ));
		$this->display();
	}
	
	/************ajax修改商品数量*****************/

	public function ajaxEditGoodsNumber()
	{
		//var_dump($_GET);die;
		 $cartModel = D('Cart');
		if($cartModel->editCartNumber(I('get.goods_id'),I('goods_number')))
		{
			echo json_encode(array(
					'ok' => 1,
			));
		}
		else
		{
			echo json_encode(array(
					'ok' => 0,
					'error' => $cartModel->getError(),
			));
		}
	} 
	
	//删除购物车终端商品
	public function delete(){
		$id = I('get.id');
		//var_dump($id);die();
		$cartModel = D('Cart');
		if($cartModel->delete($id)!== false){
			$this->success('删除成功');
			exit;
		}else{
			$this->error($model->getError);
		}
	}
	//购物车中的AJAX
	public function ajaxCartlist(){
		$cartModel  = D('Home/cart');
		$data = $cartModel->cartList();
		echo json_encode($data);
	} 
}